//
//  JYJVCOfDiscover.m
//  HappyListen
//
//  Created by 冀永金 on 16/8/27.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "JYJVCOfDiscover.h"
#import "BaseCollectionView.h"
#import "BaseCollectionViewCell.h"
#import "JYJTAMacro.h"
#import "JYJSmallCollectionViewCell.h"
#import "BaseLabel.h"
#import "JYJBigCollectionViewCell.h"
#import "JYJBigSortCollectionViewCell.h"
#import "JYJBigCollectionViewCellTWo.h"
#import "JYJVCOfAnchorInfoViewController.h"
#import "JYJVCOfSortContent.h"
#import "JYJMusicPlayerViewController.h"
#import "JYJVCOfFeaturedTopThreeDetails.h"
#import "JYJNetWorkParser.h"
#import "JYJVCOfSeach.h"
#import "JYJMusicMiniView.h"
@interface JYJVCOfDiscover ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) BaseCollectionView *samllCollectionView;
@property (nonatomic, strong) BaseCollectionView *bigCollectionView;
@property (nonatomic, strong) UIView *redView;
@property (nonatomic, assign) NSInteger labelIndex;//记录label的位置
@property (nonatomic, strong) NSIndexPath *oldIndePath;//记录上一个indexPath;
@end

@implementation JYJVCOfDiscover

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"发现";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"menu_icon_4_1"] style:UIBarButtonItemStylePlain target:self  action:@selector(Seach)];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.view.backgroundColor = [UIColor colorWithRed:0.07 green:0.07 blue:0.08 alpha:1.00];
    self.oldIndePath = [NSIndexPath indexPathForItem:0 inSection:0];
    [self createSmallCollection];
    [self createBigCollectionView];
    [self createRedView];
    [self createKVOAction];
    // Do any additional setup after loading the view.
}
- (void)createSmallCollection {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH / 2, 50);
    layout.minimumInteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.scrollDirection = UICollectionViewScrollPositionCenteredHorizontally;
    self.samllCollectionView = [[BaseCollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50) collectionViewLayout:layout];
    [self.view addSubview:_samllCollectionView];
    self.samllCollectionView.delegate = self;
    self.samllCollectionView.dataSource = self;
    [self.samllCollectionView registerClass:[JYJSmallCollectionViewCell class] forCellWithReuseIdentifier:@"Small"];
    self.samllCollectionView.scrollEnabled = NO;
    self.samllCollectionView.showsHorizontalScrollIndicator = NO;
}
- (void)createBigCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49 - 53);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumLineSpacing = 0;
    self.bigCollectionView = [[BaseCollectionView alloc]initWithFrame:CGRectMake(0, 53, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49 - 53) collectionViewLayout:layout];
    [self.view addSubview:_bigCollectionView];
    self.bigCollectionView.delegate = self;
    self.bigCollectionView.dataSource = self;
    [self.bigCollectionView registerClass:[JYJBigCollectionViewCell class] forCellWithReuseIdentifier:@"Featured"];
    self.bigCollectionView.showsHorizontalScrollIndicator = NO;
    [self.bigCollectionView registerClass:[JYJBigCollectionViewCellTWo class] forCellWithReuseIdentifier:@"Sort"];
    self.bigCollectionView.pagingEnabled = YES;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 2;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.samllCollectionView) {
        JYJSmallCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Small" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor colorWithRed:0.09 green:0.09 blue:0.11 alpha:1.00];
        NSArray *arr = @[@"精选集",@"分类"];
        //保证开启程序字体是和白色
        if (indexPath.row == _labelIndex) {
            cell.nameLabel.textColor = [UIColor whiteColor];
        }else {
            cell.nameLabel.textColor = [UIColor lightGrayColor];
        }
        if (indexPath.row == _oldIndePath.item) {
            cell.nameLabel.textColor = [UIColor whiteColor];
        }else {
            cell.nameLabel.textColor = [UIColor lightGrayColor];
        }
        cell.nameLabel.text = arr[indexPath.item];
        return cell;
    }else {
        if (indexPath.item == 0) {
            JYJBigCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Featured" forIndexPath:indexPath];
            cell.backgroundColor = [UIColor lightGrayColor];
            // block
           __weak typeof (self) weakself = self;
            cell.handleAction = ^(JYJVCOfAnchorInfoViewController *info){
                weakself.tabBarController.tabBar.hidden = YES;
                weakself.hidesBottomBarWhenPushed = YES;
                [weakself.navigationController pushViewController:info animated:YES];
               weakself.hidesBottomBarWhenPushed = NO;
            };
            cell.handleBlock = ^(JYJVCOfSortContent *sortContent) {
                weakself.tabBarController.tabBar.hidden = YES;
                weakself.hidesBottomBarWhenPushed = YES;
                [weakself.navigationController pushViewController:sortContent animated:YES];
                weakself.hidesBottomBarWhenPushed = NO;
            };
            cell.handleVC = ^(JYJVCOfFeaturedTopThreeDetails *detailsVC){
                weakself.tabBarController.tabBar.hidden = YES;
                weakself.hidesBottomBarWhenPushed = YES;
                [weakself.navigationController pushViewController:detailsVC animated:YES];
                weakself.hidesBottomBarWhenPushed = NO;
            };
            return cell;
        }else {
            JYJBigCollectionViewCellTWo *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Sort" forIndexPath:indexPath];
            // block
            __weak typeof (self) weakself = self;
            cell.handleAction = ^(JYJVCOfSortContent *sortContent) {
                weakself.tabBarController.tabBar.hidden = YES;
                weakself.hidesBottomBarWhenPushed = YES;
                [weakself.navigationController pushViewController:sortContent animated:YES];
                weakself.hidesBottomBarWhenPushed = NO;

            };
            return cell;
        }
    }
}
- (void)createRedView {
    self.redView = [[UIView alloc]initWithFrame:CGRectMake(0, 50, SCREEN_WIDTH / 2, 3)];
    _redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_redView];
}
// 添加观察者
- (void)createKVOAction {
    [self.bigCollectionView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    CGFloat X = [[change objectForKey:@"new"]CGPointValue].x;
    CGFloat redX = X / 2;
    self.redView.transform = CGAffineTransformMakeTranslation(redX, 0);

}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == self.bigCollectionView) {
        //当前大Collection的第几个item
        NSInteger currentItem = scrollView.contentOffset.x / SCREEN_WIDTH;
        // 根据第几个item获取当前的indexPath
        NSIndexPath *currentIndexPath = [NSIndexPath indexPathForItem:currentItem inSection:0];
        // 根据indexpaht获取当前的标题的Collection
        JYJSmallCollectionViewCell *cell = (JYJSmallCollectionViewCell *)[self.samllCollectionView cellForItemAtIndexPath:currentIndexPath];
        //字体变色
        cell.nameLabel.textColor = [UIColor whiteColor];
        // 恢复字体颜色
        NSIndexPath *indexPathPast = [NSIndexPath indexPathForItem:_labelIndex inSection:0];
        //加判防止滑一点点就就开，并没有划入新页面，字体就变色了
        if (currentIndexPath != indexPathPast) {
            JYJSmallCollectionViewCell *cell = (JYJSmallCollectionViewCell *)[self.samllCollectionView cellForItemAtIndexPath:indexPathPast];
            cell.nameLabel.textColor = [UIColor lightGrayColor];
        }
        //记录当前的item 和 划过的indexPath
        self.labelIndex = currentItem;
        self.oldIndePath = currentIndexPath;
    }
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.samllCollectionView) {
        [self.bigCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
        JYJSmallCollectionViewCell *cell = (JYJSmallCollectionViewCell *)[self.samllCollectionView cellForItemAtIndexPath:indexPath];
        cell.nameLabel.textColor = [UIColor whiteColor];
        JYJSmallCollectionViewCell *pastCell = (JYJSmallCollectionViewCell *)[self.samllCollectionView cellForItemAtIndexPath:_oldIndePath];
        if (indexPath != _oldIndePath) {
            pastCell.nameLabel.textColor = [UIColor lightGrayColor];
        }
        self.oldIndePath = indexPath;
        self.labelIndex = _oldIndePath.item;
    }
}
- (void)dealloc {
    [self.bigCollectionView removeObserver:self forKeyPath:@"contentOffset"];
    
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    self.hidesBottomBarWhenPushed = NO;
    [JYJMusicMiniView shareMiniView].hidden = NO;
}
- (void)Seach {
    JYJVCOfSeach *seach = [[JYJVCOfSeach alloc]init];
    self.tabBarController.tabBar.hidden = YES;
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:seach animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
